﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VOL;
using BLL;

namespace Web.Admin
{
    public partial class LoaiHangForm : System.Web.UI.Page
    {
        LoaiHangBL _LoaiHangBLL = new LoaiHangBL();
        int MaLoaiHang = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string _MaLoaiHang = Request.QueryString["MaLoaiHang"];

                if (_MaLoaiHang == null || _MaLoaiHang == "" || !int.TryParse(_MaLoaiHang, out MaLoaiHang))
                {
                    hdfMaLoaiHang.Value = "0";
                }
                else
                {
                    LoaiHang obj = new LoaiHang();
                    obj.MaLoaiHang = MaLoaiHang;
                    DataTable dt = _LoaiHangBLL.searchIDLoaiHang(obj);

                    txtTenLoaiHang.Text = dt.Rows[0]["TenLoaiHang"].ToString();
                    hdfMaLoaiHang.Value = _MaLoaiHang;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string _MaLoaiHang = hdfMaLoaiHang.Value;
            if (_MaLoaiHang == null || _MaLoaiHang == "0")
            {
                // thực hiện thêm mới
                LoaiHang obj = new LoaiHang();
                obj.TenLoaiHang = txtTenLoaiHang.Text;
                _LoaiHangBLL.insertLoaiHang(obj);
            }
            else
            {
                //// thực hiện sửa
                LoaiHang obj = new LoaiHang();
                obj.MaLoaiHang = int.Parse(_MaLoaiHang);
                obj.TenLoaiHang = txtTenLoaiHang.Text;
                _LoaiHangBLL.updateLoaiHang(obj);
            }

            Response.Redirect("~/Admin/LoaiHangManager.aspx");
        }
    }
}